package cn.marinda.resturants.controller;

import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import org.springframework.http.HttpRequest;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URL;

/**
 * @author hwj
 */
@RestController
@RequestMapping("/uploads")
public class FileController {

    @RequestMapping("/file")
    public ApiResult getServer(@RequestParam("file") MultipartFile file)
    {
        String staticUrl = "D://upload";
        String fileName = file.getOriginalFilename();
        String filePrefix = fileName.substring(fileName.indexOf("."));
        String uploadLoaction = staticUrl;
        File uploadFile = IOUtils.uploadFile(uploadLoaction,filePrefix);

        try {
            //上传
            file.transferTo(uploadFile);
            return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,uploadFile.getName());
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    }
